package com.xyy.saas.payment.merchant.core.service;

import com.alibaba.fastjson.JSONObject;
import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.OpenPayStatusEnum;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.dao.model.ApplypayUserInfo;
import com.xyy.saas.payment.service.ApplypayUserInfoService;
import com.xyy.saas.payment.util.ApolloUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import util.AESUtil;
import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class ApplypayUserInfoServiceV2 {
    @Resource
    private ApplypayUserInfoService applypayUserInfoService;

    /**
     * 1.保存进件信息
     * 2.检验是否能进件判断
     */
    public void insertOrUpdate(ApplypayUserInfo applypayUserInfo) throws PaymentException {
        log.info("ApplypayUserInfoServiceV2#insertOrUpdate applypayUserInfo：{}", JSONObject.toJSONString(applypayUserInfo));

        ApplypayUserInfo oldApplypayUserInfo = applypayUserInfoService.queryUserInfoByAccountId(applypayUserInfo.getAccountId());

        if(oldApplypayUserInfo == null) {
            applypayUserInfoService.insert(applypayUserInfo);
        }
        else {
            /**
             * 检验是否能进件判断，只有失败状态才能再次报件
             */
            if(!OpenPayStatusEnum.canCreate(oldApplypayUserInfo.getStatus())) {
                throw new PaymentException(ResultCodeEnum.OPEN_PAY_ALREADY_OPEN);
            }

            applypayUserInfo.setId(oldApplypayUserInfo.getId());
            applypayUserInfoService.updateByPrimaryKeySelective(applypayUserInfo);
        }
    }

    /**
     * 根据主键ID更新状态
     * @param id
     * @param status
     * @return
     */
    public boolean updateStatus(Long id, OpenPayStatusEnum status) {
        return updateStatus(id, status.getStatus(), status.getDesc());
    }

    /**
     * 根据主键ID更新状态
     * @param id
     * @param status
     * @param message
     * @return
     */
    public boolean updateStatus(Long id, Integer status, String message) {
        ApplypayUserInfo applypayUserInfo = new ApplypayUserInfo();
        applypayUserInfo.setId(id);
        applypayUserInfo.setStatus(status);
        applypayUserInfo.setMessage(message);
        return applypayUserInfoService.updateByPrimaryKeySelective(applypayUserInfo) > 0;
    }

    /**
     * 获取企业简称
     * @param accountId
     * @return
     */
    public String getCompanyShortNameByAccountId(String accountId) throws Exception {
        ApplypayUserInfo applypayUserInfo = applypayUserInfoService.queryUserInfoByAccountId(accountId);

        if(applypayUserInfo == null) {
            throw new PaymentException();
        }

        return JSONObject.parseObject(AESUtil.decrypt(applypayUserInfo.getBusinessLicenseInfo(),
                ApolloUtil.getProperty(Constants.USER_INFO_KEY))).getString("companyShortName");
    }

    /**
     * 查询报件中状态数据
     * @return
     */
    public List<ApplypayUserInfo> queryOpeningUserInfo(Integer offset) {
        return applypayUserInfoService.queryUserInfoByStatus(OpenPayStatusEnum.OPENING.getStatus(),
                offset, Constants.DEFAULT_PAGE_SIZE);
    }
}
